/*
 * libustream: User-space stackable streams
 * @(#) $Id: file.c 2 2009-02-20 09:52:11Z mo@ilaven.net $
 */

/*
 * Copyright (c) 2002, 2008 Mo McRoberts.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the author(s) of this software may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "p_usmodules.h"

/* This module implements stream head support for disk files */

static com_result_t __stdcall UStream_File_QueryInterface(IUStreamNode *intf, com_riid_t iid, void **out);
static uint32_t __stdcall UStream_File_AddRef(IUStreamNode *intf);
static uint32_t __stdcall UStream_File_Release(IUStreamNode *intf);
static com_result_t __stdcall UStream_File_Write(IUStreamNode *intf, IUStream *stream, const void *buf, size_t *buflen);
static com_result_t __stdcall UStream_File_Read(IUStreamNode *intf, IUStream *stream, void *buf, size_t *buflen);

DECLARE_CLASS(UStream_File)
{
	DECLARE_IMPLEMENTS(UStream_File, IUStreamNode);
	uint32_t refcount;
	int fd;
};

DEFINE_CLASS_INTERFACE(UStream_File, IUStreamNode)
{
	UStream_File_QueryInterface,
	UStream_File_AddRef,
	UStream_File_Release,
	UStream_Head_GetChildNode,
	UStream_File_Write,
	UStream_File_Read,
	UStream_Head_Pending,
	UStream_Head_Accept,
};

extern com_result_t
us__file_open(const iri_t *iri, int oflag, mode_t mode, IUStreamNode **out, int *fdout)
{
	UStream_File *f;
	
	if(NULL == (f = (UStream_File *) calloc(1, sizeof(UStream_File))))
	{
		return errno;
	}
	if(-1 == (f->fd = open(iri->path, oflag, mode)))
	{
		free(f);
		return errno;
	}
	INITIALISE_ADDREF_INTERFACE_POINTER(UStream_File, f, IUStreamNode);
	*out = GET_INTERFACE_POINTER(f, IUStreamNode);
	*fdout = f->fd;
	return COM_S_OK;
}

static com_result_t __stdcall
UStream_File_QueryInterface(IUStreamNode *intf, com_riid_t iid, void **out)
{
	if(com_guid_equal(iid, &IID_IUnknown) || com_guid_equal(iid, &IID_IUStreamNode))
	{
		UStream_File_AddRef(intf);
		*out = (void *) intf;
		return COM_S_OK;
	}
	return COM_E_NOINTERFACE;
}

static uint32_t __stdcall
UStream_File_AddRef(IUStreamNode *intf)
{
	DECLARE_SELF(UStream_File, IUStreamNode, intf);
	
	self->refcount++;
	return self->refcount;
}

static uint32_t __stdcall
UStream_File_Release(IUStreamNode *intf)
{
	DECLARE_SELF(UStream_File, IUStreamNode, intf);
	
	self->refcount--;
	if(0 == self->refcount)
	{
		if(self->fd != -1)
		{
			close(self->fd);
		}
		free(self);
		return 0;
	}
	return self->refcount;
}

static com_result_t __stdcall
UStream_File_Write(IUStreamNode *intf, IUStream *stream, const void *buf, size_t *buflen)
{
	ssize_t r;

	DECLARE_SELF(UStream_File, IUStreamNode, intf);

	(void) stream;
	
/*	fprintf(stderr, "Writing %d bytes\n", (int) (*buflen)); */
	do
	{
		r = write(self->fd, buf, *buflen);
	}
	while(-1 == r && EINTR == errno);
	if(-1 == r)
	{
		return errno;
	}
	/* Return the number of bytes remaining in *buflen */
	*buflen -= r;
	return COM_S_OK;
}

static com_result_t __stdcall
UStream_File_Read(IUStreamNode *intf, IUStream *stream, void *buf, size_t *buflen)
{
	ssize_t r;

	DECLARE_SELF(UStream_File, IUStreamNode, intf);

	(void) stream;
	
	do
	{
		r = read(self->fd, buf, *buflen);
	}
	while(-1 == r && EINTR == errno);
	if(-1 == r)
	{
		return errno;
	}
	if(0 == r)
	{
		/* End of file */
		return EPIPE;
	}
	/* Return the number of bytes remaining in *buflen */
	*buflen -= r;
	return COM_S_OK;
}
